package com.xiaoma.verbal.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.UserBdpushBindDao;
import com.xiaoma.tpo.domain.UserBdpushBind;
import com.xiaoma.tpo.domain.UserMessage;
import com.xiaoma.verbal.dao.PrctPracticeAudioDao;
import com.xiaoma.verbal.dao.PrctScoreRecordDao;
import com.xiaoma.verbal.domain.PrctPracticeAudio;
import com.xiaoma.verbal.domain.PrctScoreRecord;
import com.xiaoma.verbal.dto.CorrectedAudioDto;
import com.xiaoma.verbal.utils.BaiduPush;

@Service
public class PrctScoreRecordService {
	@Autowired
	private PrctPracticeAudioDao prctPracticeAudioDao;
	@Autowired
	private PrctScoreRecordDao prctScoreRecordDao;
	@Resource
	private PrctPracticeService prctPracticeService;
	@Resource
	private PrctAssistantService prctAssistantService;
	@Autowired
	private UserBdpushBindDao userBdpushBindDao;
	@Resource
	private TaskExecutor taskExecutor;
	private static final Logger LOG = LoggerFactory.getLogger(PrctScoreRecordService.class);
	public PrctScoreRecord fillScore(CorrectedAudioDto audioVo) throws Exception {
		PrctPracticeAudio audio = this.prctPracticeAudioDao.getById(audioVo.getAnswerId());
		if (audio != null) {
			PrctScoreRecord score = this.merge(audioVo);
			if (score.getUpdateTime() == score.getCreateTime()) {
				this.prctPracticeService.increaseCorrected(audio.getUserId());
			}
			audio.setUpdateTime(new Date());
			audio.setStatus(3);
			this.prctPracticeAudioDao.update(audio);
			this.prctAssistantService.merge(audioVo.getXbId(), audioVo.getXbNickName());
			return score;
		} else {
			LOG.debug("audio is missing id{" + audioVo.getAnswerId() + "}");
		}

		return null;
	}

	public PrctScoreRecord merge(CorrectedAudioDto audioVo) throws Exception {
		PrctScoreRecord bean = this.prctScoreRecordDao.getById(audioVo.getAnswerId());
		if (bean != null) {
			this.convert(bean, audioVo);
			bean.setUpdateTime(new Date());
			this.update(bean);
		} else {
			bean = new PrctScoreRecord();
			this.convert(bean, audioVo);
			bean.setCreateTime(new Date());
			bean.setUpdateTime(bean.getCreateTime());
			bean.setPracticeId(audioVo.getAnswerId());
			this.save(bean);
		}
		return bean;
	}

	public PrctScoreRecord update(PrctScoreRecord bean) {
		this.prctScoreRecordDao.update(bean);
		return bean;
	}

	public PrctScoreRecord save(PrctScoreRecord bean) {
		bean.setId(this.prctScoreRecordDao.insert(bean));
		return bean;
	}

	public PrctScoreRecord convert(PrctScoreRecord bean, CorrectedAudioDto audioVo) {
		bean.setAssistantId(audioVo.getXbId());
		bean.setComment(audioVo.getPgComment());
		bean.setComprehensiveScore(audioVo.getComprehensiveScore());
		bean.setDeliveryScore(audioVo.getDeliveryScore());
		bean.setFinishTime(audioVo.getPgTime());
		bean.setLanguageScore(audioVo.getLanguageScore());
		bean.setTopicScore(audioVo.getTopicScore());
		bean.setType(audioVo.getPgType());
		return bean;
	}

	public void pushMsgToBaidu(final CorrectedAudioDto audioVo) {
		LOG.info("百度云推送{}"+audioVo.getAnswerId());
		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				PrctPracticeAudio audio = prctPracticeAudioDao.getById(audioVo.getAnswerId());
				UserBdpushBind bind = new UserBdpushBind();
				bind.setUserId(audio.getUserId());
				List<UserBdpushBind> list = userBdpushBindDao.findByColumns(bind);
				if (!CollectionUtils.isEmpty(list)) {
					UserMessage userMessage = new UserMessage();
					userMessage.setMessage("您有新的作业批改信息！");
					bind = list.get(0);
					if (bind.getIosUserId() != null && bind.getIosChannelId() != null && bind.getIosUserId().length() > 0 && bind.getIosChannelId().length() > 0) {
						LOG.info("百度云推送 IOS {}"+audioVo.getAnswerId());
						BaiduPush.sendIosNotification(bind.getIosUserId(), Long.valueOf(bind.getIosChannelId()), userMessage);
					}
					if (bind.getAndroidUserId() != null && bind.getAndroidChannelId() != null && bind.getAndroidChannelId().length() > 0 && bind.getAndroidUserId().length() > 0) {
						LOG.info("百度云推送 Android {}"+audioVo.getAnswerId());
						BaiduPush.sendAndroidUnicastNotification(bind.getAndroidUserId(), Long.valueOf(bind.getAndroidChannelId()), userMessage);
					}

				}
			}
		});

	}

}
